package com.sunwayworld.basemodule.common.eptstorage.equiptacqschedule.service.impl;

import com.sunwayworld.basemodule.common.eptstorage.acqconfig.bean.LimsEquiptAcqConfigInstBean;
import com.sunwayworld.basemodule.common.eptstorage.acqconfig.net.NetEptSenderHelper;
import com.sunwayworld.basemodule.common.eptstorage.acqconfig.service.LimsEquiptAcqConfigInstService;
import com.sunwayworld.basemodule.common.eptstorage.acqconfig.service.LimsEquiptAcqConfigService;
import com.sunwayworld.basemodule.common.eptstorage.equiptacqschedule.bean.LimsEquiptAcqScheduleTaskBean;
import com.sunwayworld.basemodule.common.eptstorage.equiptacqschedule.dao.LimsEquiptAcqScheduleTaskDao;
import com.sunwayworld.basemodule.common.eptstorage.equiptacqschedule.service.LimsEquiptAcqScheduleTaskService;
import com.sunwayworld.basemodule.rm.equipment.service.LimsEquipmentService;
import com.sunwayworld.cloud.module.equipt.acqconfig.bean.CoreEquiptAcqConfigBean;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsEquiptAcqScheduleTaskServiceImpl implements LimsEquiptAcqScheduleTaskService {

    @Autowired
    private LimsEquiptAcqScheduleTaskDao limsEquiptAcqScheduleTaskDao;

    @Autowired
    private LimsEquiptAcqConfigInstService limsEquiptAcqConfigInstService;

    @Autowired
    private LimsEquiptAcqConfigService limsEquiptAcqConfigService;

    @Autowired
    private NetEptSenderHelper netEptSenderHelper;

    @Autowired
    private LimsEquipmentService equipmentService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsEquiptAcqScheduleTaskDao getDao() {
        return limsEquiptAcqScheduleTaskDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsEquiptAcqScheduleTaskBean limsEquiptAcqScheduleTask = jsonWrapper.parseUnique(LimsEquiptAcqScheduleTaskBean.class);
        limsEquiptAcqScheduleTask.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsEquiptAcqScheduleTask);
        return limsEquiptAcqScheduleTask.getId();
    }

    @Override
    public void sendMsgToEquipt(String frequency) {
        List<LimsEquiptAcqConfigInstBean> instBeans = limsEquiptAcqConfigInstService.selectListByFilter(SearchFilter.instance()
                .match("taskFlag","1").filter(MatchPattern.EQ)
                .match("acqFrequency",frequency).filter(MatchPattern.EQ));
        Map<String,List<LimsEquiptAcqConfigInstBean>> instBeansByEquipt = instBeans.stream()
                .collect(Collectors.groupingBy(e -> e.getAcqConfigId(), HashMap::new,Collectors.toList()));
        List<String> equiptIds = instBeansByEquipt.keySet().stream().collect(Collectors.toList());
        List<CoreEquiptAcqConfigBean> equiptAcqConfigBeans = limsEquiptAcqConfigService.getDao().selectListByIds(equiptIds);

        for (CoreEquiptAcqConfigBean equiptAcqConfigBean : equiptAcqConfigBeans){
            List<LimsEquiptAcqConfigInstBean> instBeanList = instBeansByEquipt.get(equiptAcqConfigBean.getId().toString());
            if ("net".equalsIgnoreCase(equiptAcqConfigBean.getAcqCategory())){
                netEptSenderHelper.asynSend(equiptAcqConfigBean,instBeanList);
            }else if ("mqtt".equalsIgnoreCase(equiptAcqConfigBean.getAcqCategory())){
                for (LimsEquiptAcqConfigInstBean instBean : instBeanList){
                    netEptSenderHelper.synSend(equiptAcqConfigBean,instBean);
                }
            }

        }
        //mqtt协议
        List<LimsEquiptAcqConfigInstBean> mqttInstBeans = limsEquiptAcqConfigInstService.selectListByFilter(SearchFilter.instance()
                .match("conversationRole","publish").filter(MatchPattern.EQ));
        Map<String,List<LimsEquiptAcqConfigInstBean>> mqttInstBeansByEquipt = mqttInstBeans.stream()
                .collect(Collectors.groupingBy(e -> e.getAcqConfigId(), HashMap::new,Collectors.toList()));
        List<String> mqttEquiptIds = mqttInstBeansByEquipt.keySet().stream().collect(Collectors.toList());
        List<CoreEquiptAcqConfigBean> mqttEquiptAcqConfigBeans = limsEquiptAcqConfigService.getDao().selectListByIds(mqttEquiptIds);
        for (CoreEquiptAcqConfigBean equiptAcqConfigBean : mqttEquiptAcqConfigBeans){
            List<LimsEquiptAcqConfigInstBean> instBeanList = mqttInstBeansByEquipt.get(equiptAcqConfigBean.getId().toString());
             if ("mqtt".equalsIgnoreCase(equiptAcqConfigBean.getAcqCategory())){
                for (LimsEquiptAcqConfigInstBean instBean : instBeanList){
                    netEptSenderHelper.synSend(equiptAcqConfigBean,instBean);
                }
            }
        }
    }

    @Override
    public void sendMsgToEquipt_30s() {
        sendMsgToEquipt("30");
    }

    @Override
    public void sendMsgToEquipt_45s() {
        sendMsgToEquipt("45");
    }

    @Override
    public void sendMsgToEquipt_1m() {
        sendMsgToEquipt("60");
    }

    @Override
    public void sendMsgToEquipt_5m() {
        sendMsgToEquipt("300");
    }

    @Override
    public void sendMsgToEquipt_10m() {
        sendMsgToEquipt("600");
    }
}
